package org.wangp.business.user.web.controller.advice

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody
import org.wangp.framework.core.domain.CommonResult
import org.wangp.framework.core.exception.BusinessException

/**
 * @author wangpeng (๑•ᴗ•๑)
 * @since 2021/10/20 2:04
 */
@ResponseBody
@ControllerAdvice
class ExceptionAdvice {

	companion object {
		val log: Logger = LoggerFactory.getLogger(ExceptionAdvice::class.java)
	}

	@ExceptionHandler(value = [BusinessException::class])
	fun businessExceptionHandler(ex: BusinessException): CommonResult<Void> {
		log.error("business error", ex)
		return CommonResult.exceptionFail(ex)
	}

	@ExceptionHandler(value = [Throwable::class])
	fun throwableExceptionHandler(thr: Throwable): CommonResult<Void> {
		log.error("system error", thr)
		return CommonResult.fail()
	}
}
